package com.auxgroup.hr.front.site.co.process;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.enums.WfTypeEnum;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.PsCPersonalData;
import com.auxgroup.hr.front.dao.domain.PsCWfRecHdr;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.personnal.PersonalDataService;
import com.auxgroup.hr.front.service.process.WfRecHdrService;
import com.auxgroup.hr.front.site.co.BaseWfCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.process.WfDrftHdrReqVO;
import com.auxgroup.hr.front.site.vo.process.WfRecHdrRespVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Set;

@RestController
@RequestMapping("${auxgroup.hr.contextPath}/wfRecHdr")
public class WfRecHdrCO extends BaseWfCO {

    private static final Logger logger = LoggerFactory.getLogger(WfRecHdrCO.class);

    @Autowired
    public WfRecHdrService wfRecHdrService;

    @Autowired
    private PersonalDataService personalDataService;


    @Resource(name = "dozerMapper")
    protected IObjectMapper objectMapper;


    /**
     * 获取招聘需求审批流程信息
     *
     * @param cTransactionNbr
     * @return
     */
    @GetMapping(value = "/getRecInfo")
    public BaseResponseVO getOne(String cTransactionNbr, @SessionGain("user") UserDTO user) {
        logger.info("getWfRecHdr param= {}", cTransactionNbr);
        if (StringUtils.isBlank(cTransactionNbr)) {
            return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL);
        }
        PsCWfRecHdr recHdr = wfRecHdrService.getRecHdr(cTransactionNbr);
        if (recHdr == null) {
            return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
        }
        WfRecHdrRespVO respVo = objectMapper.map(recHdr, WfRecHdrRespVO.class);

        //岗位性质短描述
        respVo.setcJobType2(sysDictService.getJobPropertyDescshort(respVo.getcJobType2()));
        //业务单位描述
        respVo.setcWfBu(sysDictService.getBusinessName(recHdr.getcWfBu()));
        //招聘负责人
        PsCPersonalData recRes = personalDataService.getPersonalData(recHdr.getcWfRecRes());
        if (recRes != null) {
            respVo.setcWfRecResName(recRes.getName());
        }

        String setid = recHdr.getcXfrSetidDept();
        if(setid == null || setid.trim().equals("")){
            setid = "SHARE";
        }

        respVo.setcXfrDeptDescr(sysDictService.getDetpFullName(setid, recHdr.getcXfrDeptid()));

        PsCPersonalData daoshi = personalDataService.getPersonalData(recHdr.getcEmplid2());
        if (daoshi != null) {
            respVo.setcEmplid2Name(daoshi.getName());
        }

        PsCPersonalData cRecSupervisor = personalDataService.getPersonalData(recHdr.getcRecSupervisor());
        if (cRecSupervisor != null) {
            respVo.setcRecSupervisorName(cRecSupervisor.getName());
        }


        fillBaseWfInfo(user, recHdr, respVo);
        return buildRespone(respVo);
    }


    /**
     * 保存并审批招聘录用流程
     *
     * @param reqVo
     * @param user
     * @param binding
     * @return
     */
    @PostMapping(value = "/approveRec")
    public BaseResponseVO approveDrft(WfDrftHdrReqVO reqVo, @SessionGain("user") UserDTO user, BindingResult binding) {
        // 表单校验
        super.assertValidation(binding);
        PsCWfRecHdr recHdr = wfRecHdrService.getRecHdr(reqVo.getcTransactionNbr());
        if (recHdr == null) {
            return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
        }
        //设置数据
//		super.fillParams(drftHdr, user, reqVo);
//        //保存数据
//        wfDrftHdrService.saveDrftHdr(drftHdr);
        //审批流程
        return requestWs(WfTypeEnum.WF_STYPE_REC.getType(), reqVo, user);
    }

    @Override
    protected Set<String> getAuthFileds() {
        return null;
    }
}
